﻿#ifndef DBSERVICE_H
#define DBSERVICE_H

#include <QtSql/QSqlDatabase>
#include "common.h"
#include "Logger.h"

#include "computer.h"
#include "computeralarmlog.h"

#define SQL_USER_NAME "face"
#define SQL_USER_PASSWD "face_passwd"
#define SQL_DB_NAME "face.db"

#define SQL_DB_VERSION 3



class DBService
{
public:
    DBService();

    int openDB();

    int checkAndUpdateDB();

    int getSysSettingValue(const QString &key, QString & value);

    int deleteSysSettingValue(const QString &key);

    int insertSysSettingValue(const QString &key, const QString &value);

    int loadAllComputers(QList<Computer> &  computers);

    int saveOneComputer(Computer &compuer);

    int deleteOneComputer(Computer &monitor);

    int saveOneComputerAlarmLog(ComputerAlarmLog & log);

    int loadComputerAlarmLogs(list<ComputerAlarmLog *> & logs, long *computerId, QString *isRead, QString *alarmType, QString *alarmMode, int maxCount = -1);

    int loadComputerAlarmLogsCount(int &count, long *computerId, QString *isRead, QString *alarmType, QString *alarmMode);

    int deleteComputerAlarmLogs(long *computerId, QString *isRead, QString *alarmType, QString *alarmMode);

    int markUnreadComputerAlarmLogsToRead(long *computerId, QString *isRead, QString *alarmType, QString *alarmMode);

private:
    int doUpdateDBFrom2_0To3_0();

    int doUpdateDBFrom1_0To2_0();

    int doUpdateDBFrom0_0To1_0();

    int createSysSettingTable();

    int getDBVersion(int & version);

    int saveDBVersion(int version);

    int doUpdateDB();



    int isTableExist(const QString& tableName, bool &exist);

    int queryForInt(const QString  & sql, int &value);

    int queryForString(const QString  & sql, QString &value);

    int execSql(const QString & sql);


    QSqlDatabase database;
};


extern DBService gDBService;

#endif // DBSERVICE_H
